package com.scsj.supermarket.view.activity.walletmodel;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.AppBarLayout;
import android.support.v4.widget.NestedScrollView;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.scsj.supermarket.R;
import com.scsj.supermarket.adapter.BalanceAdapter;
import com.scsj.supermarket.base.GetTradeRecordListBase;
import com.scsj.supermarket.bean.GetTradeRecordListBean;
import com.scsj.supermarket.customview.BalanceChoicePop.BalanceTypeWindow;
import com.scsj.supermarket.customview.MyDecoration;
import com.scsj.supermarket.presenter.GetTradeRecordListPresenter;
import com.scsj.supermarket.view.activity.baseactivitymodel.BaseActivity;

import java.util.List;

import es.dmoral.toasty.Toasty;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 余额明细
 */
public class BalanceDetailActivity extends BaseActivity implements View.OnClickListener, GetTradeRecordListBase.IView {


    private TextView mToolbarTitle;
    private RecyclerView mRecyclerview;
    private ImageView mNullIv;
    private RelativeLayout mNullLayout;
    private NestedScrollView mScrollLayout;
    private SwipeRefreshLayout mRefresh;

    private int page = 1;
    private int pageTotal = 1;
    private String type = "";
    private String time = "";
    private BalanceTypeWindow typeWindow;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    type = getType(typeWindow.getResion());
                    mToolbarTitle.setText(typeWindow.getResion() + "明细");
                    page = 1;
                    getData();
                    break;
            }
            super.handleMessage(msg);
        }
    };
    private AppBarLayout mAppbarlayout;
    private GetTradeRecordListPresenter presenter;
    private BalanceAdapter balanceAdapter;
    private List<GetTradeRecordListBean.DataBean.ListBean> list;

    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_balance_detail_layout);
        //  ScreenAdapterTools.getInstance().loadView((ViewGroup)getWindow().getDecorView());
        //ScreenAdapterTools.getInstance().reset(this);//如果希望android7.0分屏也适配的话,加上这句
        initToolbar();
        initView();
        setToolbarTitle("余额明细");
        hasBack(true);
        presenter = new GetTradeRecordListPresenter(this);

    }

    @Override
    protected void initView() {

        mToolbarTitle = (TextView) findViewById(R.id.toolbar_title);
        mToolbarTitle.setOnClickListener(this);
        mRecyclerview = (RecyclerView) findViewById(R.id.recyclerview);
        mNullIv = (ImageView) findViewById(R.id.null_iv);
        mNullLayout = (RelativeLayout) findViewById(R.id.null_layout);
        mScrollLayout = (NestedScrollView) findViewById(R.id.scroll_layout);
        mRefresh = (SwipeRefreshLayout) findViewById(R.id.refresh);
        mAppbarlayout = (AppBarLayout) findViewById(R.id.appbarlayout);
//        List mBalance = new ArrayList<BalanceBean>();
//        mBalance.add(new BalanceBean("购物消费","2016-6-6  16:6:6",666.66));
//        mBalance.add(new BalanceBean("大保健","2016-6-7  16:6:6",666.66));
//        mBalance.add(new BalanceBean("实体店消费","2016-6-8  16:6:6",666.66));
//        mBalance.add(new BalanceBean("购物消费","2016-6-9  16:6:6",-666.66));
//        mBalance.add(new BalanceBean("采购商成消费","2016-6-0  16:6:6",666.66));
//        mBalance.add(new BalanceBean("购物消费","2016-6-1  16:6:6",666.66));
//        mBalance.add(new BalanceBean("购物消费1","2016-6-2  16:6:6",-666.66));
//        mBalance.add(new BalanceBean("购物消费2","2016-6-3  16:6:6",666.66));
//        mBalance.add(new BalanceBean("购物消费","2016-6-4  16:6:6",666.66));
//        mBalance.add(new BalanceBean("购物消费3","2016-6-5  16:6:6",666.66));
//        mBalance.add(new BalanceBean("购物消费5","2016-6-6  16:6:6",666.66));
//        mBalance.add(new BalanceBean("购物消费6","2016-6-6  16:6:6",-666.66));
//        mBalance.add(new BalanceBean("购物消费7","2016-6-6  16:6:6",666.66));
//        mBalance.add(new BalanceBean("购物消费8","2016-6-6  16:6:6",666.66));
//        mBalance.add(new BalanceBean("购物消费9","2016-6-6  16:6:6",666.66));
//        mBalance.add(new BalanceBean("购物消费1","2016-6-6  16:6:6",666.66));

        mRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerview.addItemDecoration(new MyDecoration(this,LinearLayoutManager.HORIZONTAL));
//        mRecyclerview.setAdapter(balanceAdapter);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.toolbar_title:
                typeWindow.showAsDropDown(mAppbarlayout);
                break;
        }
    }

    // type:0 充值 1提现2消费3退款
    private String getType(String resion) {
        if (resion.equals("全部")) {
            return "";
        } else if (resion.equals("消费")) {
            return "1";
        } else if (resion.equals("结算")) {
            return "2";
        } else if (resion.equals("退款")) {
            return "3";
        } else if (resion.equals("充值")) {
            return "4";
        }else if (resion.equals("提现")) {
            return "5";
        }else if (resion.equals("分销")) {
            return "6";
        } else {
            return "";
        }
    }

    private void getData() {

        JSONObject ob = new JSONObject();
        // type:1 余额明细
//        tradeType 交易类型 1.支付订单; 2.结算订单; 3.退款订单；4.充值单；5.提现单；6.分销单
            ob.put("type", "1");
            ob.put("tradeType", type);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),ob.toString());
        presenter.deliverData(body);


    }



    @Override
    public void showSuccessMsg(String s, GetTradeRecordListBean getTradeRecordListBean) {
        mNullLayout.setVisibility(View.INVISIBLE);
        list = getTradeRecordListBean.getData().getList();
        balanceAdapter = new BalanceAdapter(list);
        mRecyclerview.setAdapter(balanceAdapter);
    }

    @Override
    public void showErrorMsg(String s) {
        mNullLayout.setVisibility(View.VISIBLE);
        Toasty.error(this,s).show();
    }
}
